package common;

import java.io.*;

/**
 * @author Steve Melnikoff; smeln at unimelb.edu.au
 * @version 0.1
 * This helper class provides convenience methods for reading and writing to the console
 */
public class ConsoleIO {
	
	//buffer for input
	static BufferedReader bufferedReader;
	
	/**
	 * Reads from the console
	 * @param prompt String asking for console input
	 * @return String of console entered data
	 * @exception Let the user know if there has been an IOException
	 */
	public static String read(String prompt) {
		//  prompt the user to enter their name
		if(prompt != null) System.out.print(prompt+" ");
		//  open up standard input
		if(bufferedReader == null) {
		    bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		}
		String input = "";
		//try reading some input, catch exceptions and return the input as a string
		 try {
		       input = bufferedReader.readLine();
	    } catch (IOException ioe) {
	       System.out.println("IO error trying to read from console");
	    } finally {
	    	return input;
	    }
	} //eo consoleRead
	
	/**
	 * Writes a string to the console
	 * @param input String to be displayed to the console
	 */
	static void echo(String input) {
		System.out.println(input);
	}

	/**
	 * Writes a string to the console
	 * @param input String to be displayed to the console
	 */
	static void write(String input) {
		System.out.println(input);
	}

} //eo ConsoleIO
